import { UIConfig } from "../../SysConfig/SysConfig";
import UIManage from "./UIManage";


export default class UIMaskBg {
    private static Instance: UIMaskBg = null;

    public static GetInstance() {
        if (UIMaskBg.Instance == null) {
            UIMaskBg.Instance = new UIMaskBg();
        }

        return UIMaskBg.Instance;
    }

    /**背景遮罩节点 */
    private maskNode: cc.Node = null;
    private m_ui_uid: number = -1;

    /**
     * 初始化MaskNode节点
     */
    public init() {
        if (this.maskNode == null) {
            this.maskNode = new cc.Node('maskNode');
            this.maskNode.width = cc.view.getVisibleSize().width;
            this.maskNode.height = cc.view.getVisibleSize().height;
            this.maskNode.active = false;
            this.maskNode.scale = 20;

            let texture = this._getSingleTexture();
            let sprite = this.maskNode.addComponent(cc.Sprite);
            sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            sprite.spriteFrame = new cc.SpriteFrame(texture);
        }
    }

    private _getSingleTexture() {
        let data: any = new Uint8Array(2 * 2 * 4);
        for (let i = 0; i < 2; i++) {
            for (let j = 0; j < 2; j++) {
                data[i * 2 * 4 + j * 4 + 0] = 0;
                data[i * 2 * 4 + j * 4 + 1] = 0;
                data[i * 2 * 4 + j * 4 + 2] = 0;
                data[i * 2 * 4 + j * 4 + 3] = 255;
            }
        }
        let texture = new cc.Texture2D();
        texture.initWithData(data, cc.Texture2D.PixelFormat.RGBA8888, 2, 2);
        //texture.handleLoadedTexture();
        return texture;
    }

    /**
     * 展示遮罩背景
     * @param ui_uid 
     */
    public showMaskBg(ui_uid: UIConfig.UI_TYPE_ID) {
        if (!cc.isValid(this.maskNode, true)) { return }
        
        this.m_ui_uid = -1;
        let uiPlane = UIManage.Instance.getUIPlaneById(ui_uid);
        let uiPlaneInfo = UIManage.Instance.getUIPlaneInfoById(ui_uid);
        if (uiPlaneInfo) {
            let uiMaskBg = uiPlaneInfo._uiMaskBg;
            if (uiMaskBg != 3) {
                this.m_ui_uid = ui_uid;
                this.maskNode.parent = uiPlane || UIManage.Instance.getGameLanchNode();
            }

            this.maskNode.zIndex = -1000;
            this._showModal(uiMaskBg);
        }
    }

    /**
     * 隐藏遮罩背景
     * @param ui_uid 
     */
    public hideMaskBg(ui_uid: UIConfig.UI_TYPE_ID) {
        if (!cc.isValid(this.maskNode, true)) { return }

        let uiPlane = UIManage.Instance.getNextUIPlaneById();
        let uiPlaneInfo = UIManage.Instance.getUIPlaneInfoById(ui_uid);
        if (uiPlaneInfo) {
            let uiMaskBg = uiPlaneInfo._uiMaskBg;

            //避免1级面板关闭的同时，打开了1级别面导致maskNode隐藏
            if (uiMaskBg != 3) {
                if (uiPlane) {
                    this.maskNode.parent = uiPlane;
                    this._showModal(uiMaskBg);
                }
                else {
                    this.maskNode.parent = UIManage.Instance.getGameLanchNode();
                    this.maskNode.active = false;
                }
            }
        }
    }

    private _showModal(lucenyType: number) {
        this.maskNode.off(cc.Node.EventType.TOUCH_START, this._touchStart, this);
        this.maskNode.off(cc.Node.EventType.TOUCH_END, this._touchEnd, this);
        switch (lucenyType) {
            case 1:
                this.maskNode.active = true;
                this.maskNode.opacity = 128;
                this.maskNode.on(cc.Node.EventType.TOUCH_START, this._touchStart, this);
                this.maskNode.on(cc.Node.EventType.TOUCH_END, this._touchEnd, this);
                break;
            case 2:
                this.maskNode.active = true;
                this.maskNode.opacity = 128;
                break;
            case 3:
                this.maskNode.active = false;
                this.maskNode.opacity = 0;
                break;
        }
    }


    private _touchStart() {
        //console.log('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++))')
    }

    private _touchEnd() {
        if (this.m_ui_uid != -1) {
            UIManage.Instance.DelayReleaseUIPannel(this.m_ui_uid);
            this.m_ui_uid = -1;
        }
    }
}
